use BasStoneReplication.dta, clear

* This llik normalizes UV1 = 0 uI2=0
capture program drop partobs2_lf
program define partobs2_lf
        version 8.0
	args 	lnf uV2 uV3 uI3	
	tempvar pV pI 
	qui gen double `pI' = norm((`uI3')/sqrt(2))
	qui gen double `pV' = norm((`pI'*`uV3' + (1-`pI')*`uV2')/sqrt(2))

	qui replace `lnf' = ln(`pV') + ln(`pI')   if $ML_y1 == 1
	qui replace `lnf' = ln(1-`pV'*`pI') if $ML_y1 == 0
	qui replace `lnf' = ln(`pV') + ln(1-`pI') if $ML_y1 == 2
	qui replace `lnf' = ln(1-`pV') if $ML_y1 == 3	
end




* Table 1 in the paper (Full Model)
set more off
set seed 1234567

ml model lf partobs2_lf (uV2:stepdown3 = catr mil mtnest elf lnten idealpointdi) (uV3:stepdown3 =  interconfinc intraconfinc hostlev_mn gdppcgr gdppc totalnumberofcrisis2) (uI3: stepdown3 = mil idealpointdi upopr perspercap2 poltr pcatr lagout gdppc)
matrix inits = (3.249653,  2.372404, -.0104462, -1.129891, -7.330102,  2.317568,   55.6942, -1.557992,  .0550265,  .4624153, -7.680818, -.0256894,   .205055, -.0709777, -.8286697,  .4616707,   .938277,  .1427571, -.7341128, -2.035021,  1.352735,  .1079354, -.6019921)
ml init inits, copy skip
ml maximize, difficult iterate(200)

gen samprep = e(sample)


* this is the predicted value, which will be updated below further
predictnl measure = normal(xb(#3)/sqrt(2)), ci(measure_down measure_up) se(measure_se)


gen measure2 =measure
gen measure2_se = measure_se


* if non-competitive elections, code as non-democ
replace measure2=0 if compincelec==0 | lagcompinc==0
replace measure2 = 0  if elecmean2==0 & measure2==.

replace measure2_se=0 if compincelec==0 | lagcompinc==0
replace measure2_se=0  if elecmean2==0 & measure2==.


* if non-competitive elections, code as non-democ
gen measure3 = measure
replace measure3 = . if  compincelec==0 | lagcompinc==0

gen measure4= measure2
gen measure4_se= measure2_se


*code these countries as non-democracies

replace measure4 = 0 if ccode ==710
replace measure4 = 0 if ccode ==511
replace measure4 = 0 if ccode ==520
replace measure4 = 0 if ccode ==531
replace measure4 = 0 if ccode ==572
replace measure4 = 0 if ccode ==591
replace measure4 = 0 if ccode ==620
replace measure4 = 0 if ccode ==652
replace measure4 = 0 if ccode ==663
replace measure4 = 0 if ccode ==670
replace measure4 = 0 if ccode ==680
replace measure4 = 0 if ccode ==690
replace measure4 = 0 if ccode ==692
replace measure4 = 0 if ccode ==694
replace measure4 = 0 if ccode ==696
replace measure4 = 0 if ccode ==698
replace measure4 = 0 if ccode ==701
replace measure4 = 0 if ccode ==710
replace measure4 = 0 if ccode ==731
replace measure4 = 0 if ccode ==781
replace measure4 = 0 if ccode ==816
replace measure4 = 0 if ccode ==835
replace measure4 = 0 if ccode ==935
replace measure4 = 0 if ccode ==955

replace measure4_se = 0 if ccode ==710
replace measure4_se = 0 if ccode ==511
replace measure4_se = 0 if ccode ==520
replace measure4_se = 0 if ccode ==531
replace measure4_se = 0 if ccode ==572
replace measure4_se = 0 if ccode ==591
replace measure4_se = 0 if ccode ==620
replace measure4_se = 0 if ccode ==652
replace measure4_se = 0 if ccode ==663
replace measure4_se = 0 if ccode ==670
replace measure4_se = 0 if ccode ==680
replace measure4_se = 0 if ccode ==690
replace measure4_se = 0 if ccode ==692
replace measure4_se = 0 if ccode ==694
replace measure4_se = 0 if ccode ==696
replace measure4_se = 0 if ccode ==698
replace measure4_se = 0 if ccode ==701
replace measure4_se = 0 if ccode ==710
replace measure4_se = 0 if ccode ==731
replace measure4_se = 0 if ccode ==781
replace measure4_se = 0 if ccode ==816
replace measure4_se = 0 if ccode ==835
replace measure4_se = 0 if ccode ==935
replace measure4_se = 0 if ccode ==955


gen measure5 = measure4
gen measure5_se = measure4_se

* if leader steps down, code as democracy
 
replace measure5 = 1 if stepdown3==1
replace measure5_se = 0 if stepdown3==1


*just estimates, none others

gen measure6 = measure
gen measure6_se = measure_se

replace measure6=. if compincelec==0 | lagcompinc==0
replace measure6 = .  if elecmean2==0 & measure2==.

replace measure6_se=. if compincelec==0 | lagcompinc==0
replace measure6_se=.  if elecmean2==0 & measure2==.

replace measure6 = . if stepdown3==1
replace measure6_se = . if stepdown3==1

replace measure6 = . if ccode ==710
replace measure6 = . if ccode ==511
replace measure6 = . if ccode ==520
replace measure6 = . if ccode ==531
replace measure6 = . if ccode ==572
replace measure6 = . if ccode ==591
replace measure6 = . if ccode ==620
replace measure6 = . if ccode ==652
replace measure6 = . if ccode ==663
replace measure6 = . if ccode ==670
replace measure6 = . if ccode ==680
replace measure6 = . if ccode ==690
replace measure6 = . if ccode ==692
replace measure6 = . if ccode ==694
replace measure6 = . if ccode ==696
replace measure6 = . if ccode ==698
replace measure6 = . if ccode ==701
replace measure6 = . if ccode ==710
replace measure6 = . if ccode ==731
replace measure6 = . if ccode ==781
replace measure6 = . if ccode ==816
replace measure6 = . if ccode ==835
replace measure6 = . if ccode ==935
replace measure6 = . if ccode ==955

replace measure6_se = . if ccode ==710
replace measure6_se = . if ccode ==511
replace measure6_se = . if ccode ==520
replace measure6_se = . if ccode ==531
replace measure6_se = . if ccode ==572
replace measure6_se = . if ccode ==591
replace measure6_se = . if ccode ==620
replace measure6_se = . if ccode ==652
replace measure6_se = . if ccode ==663
replace measure6_se = . if ccode ==670
replace measure6_se = . if ccode ==680
replace measure6_se = . if ccode ==690
replace measure6_se = . if ccode ==692
replace measure6_se = . if ccode ==694
replace measure6_se = . if ccode ==696
replace measure6_se = . if ccode ==698
replace measure6_se = . if ccode ==701
replace measure6_se = . if ccode ==710
replace measure6_se = . if ccode ==731
replace measure6_se = . if ccode ==781
replace measure6_se = . if ccode ==816
replace measure6_se = . if ccode ==835
replace measure6_se = . if ccode ==935
replace measure6_se = . if ccode ==955


set seed 7654321
gen rando = uniform()
gen insamp = rando <.8

* Table 1 in the paper (first stage)
set more off
ml model lf partobs2_lf (uV2:stepdown3 = catr mil mtnest elf lnten idealpointdi) (uV3:stepdown3 =  interconfinc intraconfinc hostlev_mn gdppcgr gdppc totalnumberofcrisis2) (uI3: stepdown3 = mil idealpointdi upopr perspercap2 poltr pcatr lagout gdppc) if insamp==1
ml search, r(10000)
ml maximize, difficult iterate(500)
*gen samprep = e(sample)


* this is the predicted value, which will be updated below further
predictnl s_measure = normal(xb(#3)/sqrt(2)), ci(s_measure_down s_measure_up) se(s_measure_se)


gen s_measure2 =s_measure
gen s_measure2_se = s_measure_se

* if non-competitive elections, code as non-democ
replace s_measure2=0 if compincelec==0 | lagcompinc==0
replace s_measure2 = 0  if elecmean2==0 & s_measure2==.

replace s_measure2_se=0 if compincelec==0 | lagcompinc==0
replace s_measure2_se=0  if elecmean2==0 & s_measure2==.


* if non-competitive elections, code as non-democ
gen s_measure3 = s_measure
replace s_measure3 = . if  compincelec==0 | lagcompinc==0

gen s_measure4= s_measure2
gen s_measure4_se= s_measure2_se


*code these countries as non-democracies

replace s_measure4 = 0 if ccode ==710
replace s_measure4 = 0 if ccode ==511
replace s_measure4 = 0 if ccode ==520
replace s_measure4 = 0 if ccode ==531
replace s_measure4 = 0 if ccode ==572
replace s_measure4 = 0 if ccode ==591
replace s_measure4 = 0 if ccode ==620
replace s_measure4 = 0 if ccode ==652
replace s_measure4 = 0 if ccode ==663
replace s_measure4 = 0 if ccode ==670
replace s_measure4 = 0 if ccode ==680
replace s_measure4 = 0 if ccode ==690
replace s_measure4 = 0 if ccode ==692
replace s_measure4 = 0 if ccode ==694
replace s_measure4 = 0 if ccode ==696
replace s_measure4 = 0 if ccode ==698
replace s_measure4 = 0 if ccode ==701
replace s_measure4 = 0 if ccode ==710
replace s_measure4 = 0 if ccode ==731
replace s_measure4 = 0 if ccode ==781
replace s_measure4 = 0 if ccode ==816
replace s_measure4 = 0 if ccode ==835
replace s_measure4 = 0 if ccode ==935
replace s_measure4 = 0 if ccode ==955

replace s_measure4_se = 0 if ccode ==710
replace s_measure4_se = 0 if ccode ==511
replace s_measure4_se = 0 if ccode ==520
replace s_measure4_se = 0 if ccode ==531
replace s_measure4_se = 0 if ccode ==572
replace s_measure4_se = 0 if ccode ==591
replace s_measure4_se = 0 if ccode ==620
replace s_measure4_se = 0 if ccode ==652
replace s_measure4_se = 0 if ccode ==663
replace s_measure4_se = 0 if ccode ==670
replace s_measure4_se = 0 if ccode ==680
replace s_measure4_se = 0 if ccode ==690
replace s_measure4_se = 0 if ccode ==692
replace s_measure4_se = 0 if ccode ==694
replace s_measure4_se = 0 if ccode ==696
replace s_measure4_se = 0 if ccode ==698
replace s_measure4_se = 0 if ccode ==701
replace s_measure4_se = 0 if ccode ==710
replace s_measure4_se = 0 if ccode ==731
replace s_measure4_se = 0 if ccode ==781
replace s_measure4_se = 0 if ccode ==816
replace s_measure4_se = 0 if ccode ==835
replace s_measure4_se = 0 if ccode ==935
replace s_measure4_se = 0 if ccode ==955


gen s_measure5 = s_measure4
gen s_measure5_se = s_measure4_se

* if leader steps down, code as democracy> This is the measure we use for growth etc regressions. For countries with more than one obs in a given year, convert it to country-year first 
replace s_measure5 = 1 if stepdown3==1
replace s_measure5_se = 0 if stepdown3==1


*just estimates, none others

gen s_measure6 = s_measure
gen s_measure6_se = s_measure_se

replace s_measure6=. if compincelec==0 | lagcompinc==0
replace s_measure6 = .  if elecmean2==0 & measure2==.

replace s_measure6_se=. if compincelec==0 | lagcompinc==0
replace s_measure6_se=.  if elecmean2==0 & measure2==.

replace s_measure6 = . if stepdown3==1
replace s_measure6_se = . if stepdown3==1

replace s_measure6 = . if ccode ==710
replace s_measure6 = . if ccode ==511
replace s_measure6 = . if ccode ==520
replace s_measure6 = . if ccode ==531
replace s_measure6 = . if ccode ==572
replace s_measure6 = . if ccode ==591
replace s_measure6 = . if ccode ==620
replace s_measure6 = . if ccode ==652
replace s_measure6 = . if ccode ==663
replace s_measure6 = . if ccode ==670
replace s_measure6 = . if ccode ==680
replace s_measure6 = . if ccode ==690
replace s_measure6 = . if ccode ==692
replace s_measure6 = . if ccode ==694
replace s_measure6 = . if ccode ==696
replace s_measure6 = . if ccode ==698
replace s_measure6 = . if ccode ==701
replace s_measure6 = . if ccode ==710
replace s_measure6 = . if ccode ==731
replace s_measure6 = . if ccode ==781
replace s_measure6 = . if ccode ==816
replace s_measure6 = . if ccode ==835
replace s_measure6 = . if ccode ==935
replace s_measure6 = . if ccode ==955

replace s_measure6_se = . if ccode ==710
replace s_measure6_se = . if ccode ==511
replace s_measure6_se = . if ccode ==520
replace s_measure6_se = . if ccode ==531
replace s_measure6_se = . if ccode ==572
replace s_measure6_se = . if ccode ==591
replace s_measure6_se = . if ccode ==620
replace s_measure6_se = . if ccode ==652
replace s_measure6_se = . if ccode ==663
replace s_measure6_se = . if ccode ==670
replace s_measure6_se = . if ccode ==680
replace s_measure6_se = . if ccode ==690
replace s_measure6_se = . if ccode ==692
replace s_measure6_se = . if ccode ==694
replace s_measure6_se = . if ccode ==696
replace s_measure6_se = . if ccode ==698
replace s_measure6_se = . if ccode ==701
replace s_measure6_se = . if ccode ==710
replace s_measure6_se = . if ccode ==731
replace s_measure6_se = . if ccode ==781
replace s_measure6_se = . if ccode ==816
replace s_measure6_se = . if ccode ==835
replace s_measure6_se = . if ccode ==935
replace s_measure6_se = . if ccode ==955





cor measure s_measure if insamp==0
cor measure5 s_measure5 if insamp==0
cor measure6 s_measure6 if insamp==0


* scatterplot of full and out of sample with local poly fit for Figure 7
twoway (scatter measure6 s_measure6 if insamp==0,  msize(tiny) color(black)) (lpolyci measure6 s_measure6 if insamp==0, lcolor(black)), leg(off) ytitle("Full Sample Democracy") xtitle("Out of Sample Democracy")



